/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.himaci.gtbattle.view;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.himaci.framework.BaseScreen;
import com.himaci.gtbattle.GTBattle;
import com.himaci.gtbattle.controller.GameplayController;
import com.himaci.gtbattle.model.Logger;
import com.himaci.gtbattle.resource.Assets;

/**
 *
 * @author XNPSDN
 */
public class GameplayScreen extends BaseScreen {

    public static int STATE_PLAY = 0;
    public static int STATE_PAUSED = 1;
    public static int STATE_GAME_OVER = 2;
    Vector3 touchPoint;
    Rectangle pause;
    Rectangle resumeFromPause;
    Rectangle homeFromPause;
    Rectangle homeFromGameOver;
    int state;

    public GameplayScreen(Game game) {
	super(game);

	touchPoint = new Vector3();
	pause = new Rectangle(445, 285, 30, 30);
	resumeFromPause = new Rectangle(150, 120, 80, 40);
	homeFromPause = new Rectangle(250, 120, 80, 40);
	homeFromGameOver = new Rectangle(200, 100, 80, 40);

	GameplayController.getInstance().set(batcher, camera);
    }

    public void reset() {
	state = STATE_PLAY;
	Logger.getInstance().clear();
	
	System.out.println("");
	System.out.println("GAME STARTED!!!");
    }

    @Override
    public void update(float deltaTime) {
	// kalau lagi di pause
	if (state == STATE_PLAY) {
	    // update gameplay
	    GameplayController.getInstance().update(deltaTime);

	    // check for input
	    if (Gdx.input.justTouched()) {
		camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
		if (pause.contains(touchPoint.x, touchPoint.y)) {
		    state = STATE_PAUSED;
		} else {
		    // touchnya masuk ke dalam game
		    GameplayController.getInstance().touch(Gdx.input.getX(), Gdx.input.getY());
		}
	    }

	    // cek apakah game over
	    if (GameplayController.getInstance().isGameOver() != -1) {
		state = STATE_GAME_OVER;
	    }
	} else if (state == STATE_PAUSED) {
	    // check for input
	    if (Gdx.input.justTouched()) {
		camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
		if (resumeFromPause.contains(touchPoint.x, touchPoint.y)) {
		    state = STATE_PLAY;
		} else if (homeFromPause.contains(touchPoint.x, touchPoint.y)) {
		    game.setScreen(GTBattle.mainMenuScreen);
		}
	    }
	} else if (state == STATE_GAME_OVER) {
	    // check for input
	    if (Gdx.input.justTouched()) {
		camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
		if (homeFromGameOver.contains(touchPoint.x, touchPoint.y)) {
		    game.setScreen(GTBattle.mainMenuScreen);
		}
	    }
	}
    }

    @Override
    public void present(float deltaTime) {
	Gdx.gl.glClearColor(Color.WHITE.r, Color.WHITE.g, Color.WHITE.b, 1);
	Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

	camera.update();
	batcher.setProjectionMatrix(camera.combined);

	batcher.enableBlending();
	batcher.begin();

	// render gameplay
	GameplayController.getInstance().render();

	if (state == STATE_PLAY) {
	    // pause digambar	    
	    batcher.draw(Assets.getInstance().pauseButton, pause.x, pause.y, pause.width, pause.height);	    
	} else if (state == STATE_PAUSED) {
	    // gambar pause
	    batcher.draw(Assets.getInstance().pauseBox, 240 - 100, 160 - 50, 200, 100);
	    batcher.draw(Assets.getInstance().resumeButton, resumeFromPause.x, resumeFromPause.y, resumeFromPause.width, resumeFromPause.height);
	    batcher.draw(Assets.getInstance().homeButton, homeFromPause.x, homeFromPause.y, homeFromPause.width, homeFromPause.height);
	} else if (state == STATE_GAME_OVER) {
	    // gambar winner ama button	    
	    batcher.draw(Assets.getInstance().dialogBox, 240 - 150, 160 - 100, 300, 200);
	    batcher.draw(Assets.getInstance().homeButton, homeFromGameOver.x, homeFromGameOver.y, homeFromGameOver.width, homeFromGameOver.height);	
	    BitmapFont fontImage = Assets.getInstance().fontNormal;
	    String text;	    
	    if (GameplayController.getInstance().isGameOver() == 1) {
		text = "Anda Menang!!!";
	    } else if (GameplayController.getInstance().isGameOver() == 2) {
		text = "Anda Kalah!!!";
	    } else {
		text = "Seri!!!";
	    }	    
	    fontImage.setScale(1.0f);
	    fontImage.draw(batcher, text,
		    240 - (fontImage.getBounds(text).width / 2), 210);
	}

	batcher.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void releaseResource() {
    }
}
